package lambda;

import catgory.Person;
import com.alibaba.fastjson.JSON;
import org.assertj.core.util.Lists;

import javax.swing.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.*;

/**
 * describe:
 *
 * @author huxingjie
 * @date 2021/11/19
 * @since 5.1
 */
public class FirstLambda {

    public static void main(String[] args) {
        BinaryOperator<Integer> s = (x, y) -> x + y;
        Integer apply = s.apply(1, 2);
        System.out.println(apply);
        JButton button = new JButton();
        button.addActionListener(event ->
                System.out.println(event.getActionCommand()));
        List<String> together = Stream.of(asList("1", "2"), asList("3", "4")).flatMap(numbers -> numbers.stream()).collect(toList());
//        assertEquals(asList(1, 2, 3, 4), together);
        System.out.println(together);
        int count = Stream.of(1, 2, 3).reduce(0, (acc, element) -> acc + element);
        System.out.println("count " + count);
        List<Person> personList = getPersonList();
        Person person = personList.stream().collect(Collectors.minBy(Comparator.comparing(p -> p.getId().length()))).get();
        System.out.println(person);
        Double collect = personList.stream().collect(averagingInt(p -> p.getId().length()));
        System.out.println(collect);
        Map<String, List<Person>> collect1 = personList.stream().collect(Collectors.groupingBy(Person::getName));
        System.out.println(collect1);
        String collect2 = personList.stream().map(Person::getName).collect(Collectors.joining(",", "[", "]"));
        System.out.println(collect2);
        Map<String, List<String>> collect3 = personList.stream().collect(groupingBy(Person::getName, mapping(Person::getId, toList())));
        System.out.println(collect3);
        Person person1 = personList.stream().max(Comparator.comparing(Person::getAge)).get();
        System.out.println(person1.getAge());
        Integer collect4 = personList.stream().collect(summingInt(p -> p.getAge()));
        System.out.println(collect4);
        Integer maxAge = personList.stream().reduce(0, (max, p) -> max > p.getAge() ? max : p.getAge(),
                (max1, max2) -> max1 > max2 ? max1 : max2);
        System.out.println(maxAge);
        Integer maxAge1 = personList.stream().map(Person::getAge).reduce(Integer::max).get();
        Integer sumAge1 = personList.stream().map(Person::getAge).reduce(Integer::sum).get();
        Integer sumAge2 = personList.stream().reduce(0, (sum, p) -> sum += p.getAge(), Integer::sum);
        System.out.println(sumAge1 + "," + sumAge2);
        double[] doubles = new double[]{0, 1, 2, 3, 4, 3.5};
        double[] doubles1 = simpleMovingAverage(doubles, 3);
        System.out.println(JSON.toJSONString(doubles1));

    }

    private static List<Person> getPersonList() {
        Person p1 = new Person("1", "hxj", 12);
        Person p2 = new Person("1", "hxj2", 23);
        Person p3 = new Person("3", "hxj3", 34);
        Person p4 = new Person("3", "hxj3", 37);
        List<Person> list = Lists.newArrayList(p1, p3, p2, p4);
        return list;
    }

    public static double[] parallelInitialize(int size) {
        double[] values = new double[size];
        Arrays.parallelSetAll(values, i -> i);
        double[] mewDoubles = Arrays.copyOf(values, values.length);
        return values;
    }

    //滑动平均值
    public static double[] simpleMovingAverage(double[] values, int n) {
        double[] sums = Arrays.copyOf(values, values.length);
        Arrays.parallelPrefix(sums, Double::sum);
        int start = n - 1;
        return IntStream.range(start, sums.length).parallel()
                .mapToDouble(i -> {
                    double prefix = i == start ? 0 : sums[i - n];
                    return (sums[i] - prefix) / n;
                })
                .toArray();
    }
}

